//
//  NewticketController.m
//  迷笛
//
//  Created by . on 2018/9/6.
//  Copyright © 2018年 🍃🌺🌺🍃. All rights reserved.
//

#import "MIDINewticketController.h"
#import "MIDINewtickViewCell.h"
#import "MIDINewticketWebController.h"
#import "MIDInewTicketmodel.h"
static const CGFloat MJDuration = 2.0;

@interface MIDINewticketController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView * HomeTable;
@property(nonatomic,strong)UIButton * buy;
@property(nonatomic,copy)NSMutableArray * tickerArr;
@property(nonatomic,copy)NSMutableArray * mutablearr;
@property(nonatomic,copy)NSDictionary * listDictionary;
@property(nonatomic,copy)NSMutableArray * eventsArr;


@property(nonatomic,copy)NSString * str;
@property(nonatomic,copy) NSString * event;

@property(nonatomic,strong)NSString * homeTicket;

@property(nonatomic,copy)NSMutableArray * data;

@end

@implementation MIDINewticketController

- (void)viewDidLoad {
    [super viewDidLoad];
//    UIButton * btn =[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
//    [btn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:btn];
    
    
    self.eventsArr = [NSMutableArray array];
    self.mutablearr = [NSMutableArray array];

    [self TicketAFN];
    //   黑色字变为白色
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:@selector(Block:)];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
    [self.navigationItem.leftBarButtonItem setImage:[[UIImage imageNamed:@"后退"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [self HomeUI];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return _eventsArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString * cellID = @"cell";
    MIDINewtickViewCell  * cell =[tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell)
    {
        cell =[[MIDINewtickViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        cell.backgroundColor = [UIColor colorWithHexString:@"#1B212F"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;

    }
    MIDIeventsmodel * mm2 =_eventsArr[indexPath.row];
    
    cell.mainiamge.image = [UIImage imageNamed:@"IMG_0195.jpg"];
    cell.maintext.text = @"北部湾（钦州）迷笛音乐节";
    cell.address.text =@"地址：广西北部湾";
    cell.time.text = [NSString stringWithFormat:@"时间:%@",[[[self.listDictionary objectForKey:@"events"]objectAtIndex:indexPath.row]objectForKey:@"event"]];
    cell.rmb.text = [NSString stringWithFormat:@"票价:%@",[[[self.listDictionary objectForKey:@"events"]objectAtIndex:indexPath.row]objectForKey:@"price"]];
    
    return cell;
}

-(void)TicketAFN
{
    NSString * ticke = @"ticket";
    _homeTicket = [NSString stringWithFormat:@"%@%@",MIDIApiRoot,ticke];

    [EdHttp get:_homeTicket params:nil progress:^(id downloadProgress)
     {
         NSLog(@"2222");
     }
        success:^(id responseObject)
     {
         [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
         [SVProgressHUD showWithStatus:@"加载中"];
         [SVProgressHUD dismissWithDelay:1.0];
         
         NSLog(@"票务内容%@",responseObject);
         self.mutablearr = responseObject[@"data"];
         NSLog(@"%@",self.mutablearr);
         
//         NSMutableArray *arr3 = responseObject[@"data"];
         for (int i = 0; i<[self.mutablearr count]; i++)
         {
//          data中数组events的字典内容
             self.listDictionary = [self.mutablearr objectAtIndex:i];
             NSDictionary * Dic;
             Dic = self.listDictionary[@"events"];
             NSLog(@"DIC%@",Dic);

            self.eventsArr = self.listDictionary[@"events"];

            for (Dic in self.eventsArr)
             {
//                 _event = [Dic objectForKey:@"event"];

//                     NSLog(@"event值%@",_event);

             }
             dispatch_async(dispatch_get_main_queue(), ^{
                 [self->_HomeTable reloadData];

             });
         }
     }
        failure:^(NSError *error)
     {
         NSLog(@"失败");
     }];
}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kDevice_Is_iPhoneX?HeighX(138):HeightScale(138);
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
{
    return nil;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
{
    return nil;
}

-(void)HomeUI
{
    [self.view addSubview:self.HomeTable];
    [self.view addSubview:self.buy];
    [self.buy mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(WidthScale(-10));
        make.centerX.equalTo(self.view);
        make.width.mas_offset(self.view.frame.size.width);
        make.height.mas_offset(49);
    }];
}
-(UITableView *)HomeTable
{
    if (!_HomeTable)
    {
        _HomeTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
        _HomeTable.dataSource = self;
        _HomeTable.delegate = self;
        _HomeTable.backgroundColor = [UIColor colorWithHexString:@"#171717"];
        _HomeTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_HomeTable reloadData];
    }
    return _HomeTable;
}
//视图将要出现时（导航颜色字体颜色）
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.translucent = NO;//Bar的模糊效果，默认为YES

    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"#171717"];
    self.title = @"最新票务";
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#FFFFFF"],NSFontAttributeName : [UIFont fontWithName:@"Helvetica-Bold" size:WidthScale(17)]}];
   
    //    去掉导航黑线
    UIImage *img = [UIImage imageNamed:@""];
    [self.navigationController.navigationBar setBackgroundImage:img  forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
}
// 返回上一级
-(void)Block:(UIButton * )sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MIDINewticketWebController * newticketweb = [[MIDINewticketWebController alloc]init];
    [self.navigationController pushViewController:newticketweb animated:YES];
}

@end
